package com.woniu113.junk.site.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.img.gif.NeuQuant;
import cn.hutool.core.io.FileTypeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.site.dto.AllStationInfoDTO;
import com.woniu113.junk.site.form.stationinfo.AddStationInfoForm;
import com.woniu113.junk.site.form.stationinfo.AllStationInfoForm;
import com.woniu113.junk.site.form.stationinfo.UpdateStationInfoForm;
import com.woniu113.junk.site.model.GoodsInfo;
import com.woniu113.junk.site.model.StationInfo;
import com.woniu113.junk.site.param.goodsinfo.AllGoodsInfoParam;
import com.woniu113.junk.site.param.stationinfo.AddStationInfoParam;
import com.woniu113.junk.site.param.stationinfo.AllStationInfoParam;
import com.woniu113.junk.site.param.stationinfo.UpdateStationInfoParam;
import com.woniu113.junk.site.service.StationInfoService;
import com.woniu113.junk.site.util.Data;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/stationInfo")
@Api(value = "StationInfoController", tags = "站点信息接口")
public class StationInfoController {
    @Resource
    private StationInfoService stationInfoService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增站点信息
     * @param form 新增站点信息的内容
     * @return 返回新增的站点信息,成功信息和code
     * @throws Exception
     */
    @PostMapping("/addStationInfo")
    @ApiOperation(value = "新增站点信息")
    public Result<StationInfo> addStationInfo(AddStationInfoForm form) throws Exception{
        System.out.println("站点名"+form.getStationName());
        System.out.println("站点联系方式"+form.getStationPhone());
        System.out.println("城市"+form.getCity());
        System.out.println("区/县"+form.getCounty());
        System.out.println("地址名"+form.getAddressName());
        System.out.println("经度"+form.getLongitude());
        System.out.println("纬度"+form.getLatitude());
        System.out.println("负责人id"+form.getPrincipalId());
        System.out.println("站点描述"+form.getDescription());
        System.out.println("占地面积"+form.getArea());
        System.out.println("服务范围"+form.getServiceScope());
        System.out.println("站点类型0区县级 1子级"+form.getStationType());
        System.out.println("营业执照"+form.getLicense());
        System.out.println("营业执照："+form.getLicense());
        //通过文件头信息判断文件类型
        String type = FileTypeUtil.getType(form.getLicense().getInputStream());//得到传入文件的文件类型(需要得到传入的image的输入流)
        System.out.println("图片类型："+type);
        if(!(type.equals("jpg") || type.equals("png"))){//不是这两种类型,表示传入的文件不合法
            throw new Exception("图片类型错误");
        }
        StationInfo newStationInfo = stationInfoService.addStationInfo(BeanUtil.toBean(form, AddStationInfoParam.class));
        return Result.getResult(StationInfo.class).setData(newStationInfo).setMsg("新增站点信息成功").setCode(200);
    }

    /**
     * 修改站点信息
     * @param form 修改站点信息数据
     * @return 返回成功提示和code
     */
    @PostMapping("/updateStationInfo")
    @ApiOperation(value = "修改站点信息")
    public Result updateStationInfo(@RequestBody UpdateStationInfoForm form){
        stationInfoService.updateStationInfo(BeanUtil.toBean(form, UpdateStationInfoParam.class));
        return Result.getResult().setMsg("修改站点信息成功").setCode(200);
    }

    /**
     * 暂停营业
     * @param stationId 要暂停营业的站点
     * @return 返回成功提示和code
     */
    @GetMapping("/temporarilyCloseStationInfo")
    @ApiOperation(value = "暂停营业")
    public Result temporarilyCloseStationInfo(@RequestParam("stationId") Integer stationId){
        stationInfoService.temporarilyCloseStationInfo(stationId);
        return Result.getResult().setMsg("暂停营业成功").setCode(200);
    }

    /**
     * 关闭站点
     * @param stationId 要关闭的站点
     * @return 返回成功提示和code
     */
    @GetMapping("/closeStationInfo")
    @ApiOperation(value = "关闭站点")
    public Result closeStationInfo(@RequestParam("stationId") Integer stationId){
        stationInfoService.closeStationInfo(stationId);
        return Result.getResult().setMsg("关闭成功").setCode(200);
    }

    /**
     * 根据站点id查询站点信息
     * @param stationId:站点id
     * @return 返回查询到的站点信息,成功提示和code
     */
    @GetMapping("/findStationInfoById")
    @ApiOperation(value = "根据站点id查询站点信息")
    public Result<StationInfo> findStationInfoById(@RequestParam("stationId") Integer stationId){
        StationInfo stationInfo = stationInfoService.getById(stationId);
        return Result.getResult(StationInfo.class).setData(stationInfo).setMsg("根据站点id查询站点信息成功").setCode(200);
    }

    /**
     * 查询所有站点信息
     * @param form 查询站点信息的条件
     * @return 返回查询到的站点信息,成功提示和code
     */
    @PostMapping("/findAll")
    @ApiOperation(value = "查询所有站点信息")
    public Result<List<StationInfo>> findAll(@RequestBody AllStationInfoForm form){
        System.out.println(form.getPageNum());
        System.out.println(form.getPageNum());
        System.out.println(form.getStationName());
        System.out.println(form.getStationPhone());
        int offset = (form.getPageNum()-1) *form.getPageSize();
        AllStationInfoDTO dto = stationInfoService.findAll(BeanUtil.toBean(form, AllStationInfoParam.class),offset);
        return Result.getListResult(StationInfo.class).setData(dto.getList()).setMsg("查询所有站点成功").setCode(200).setTotal(dto.getTotal());
    }

    /**
     * 根据子站点id查询区站点信息
     * @param stationId 子站点id
     * @return 返回查询到的站点信息,成功提示和code
     */
    @GetMapping("/findFatherStationById")
    @ApiOperation(value = "根据子站点id查询区站点信息")
    public Result<StationInfo> findFatherStationById(@RequestParam("stationId") Integer stationId){
        StationInfo stationInfo = stationInfoService.findFatherStationById(stationId);
        return Result.getResult(StationInfo.class).setData(stationInfo).setMsg("查询区站点成功").setCode(200);
    }

    /**
     * 根据区站点id查询子站点信息
     * @param stationId 区站点id
     * @return 返回查询到的站点信息,成功提示和code
     */
    @GetMapping("/findChildStationById")
    @ApiOperation(value = "根据区站点id查询所有子站点信息")
    public Result<List<StationInfo>> findChildStationById(@RequestParam("stationId") Integer stationId){
        List<StationInfo> stationInfos = stationInfoService.findChildStationById(stationId);
        return Result.getListResult(StationInfo.class).setData(stationInfos).setMsg("根据区站点id查询所有子站点信息").setCode(200);
    }

    /**
     * 根据(城市/区县)查询站点类型为子站点的站点信息(下单用)
     */
    @GetMapping("/findChildStationByCityAndCounty")
    @ApiOperation(value = "根据(城市/区县)查询站点类型为子站点的站点信息")
    public Result<List<StationInfo>> findChildStationByCityAndCounty(@RequestParam("city") String city, @RequestParam("county") String county){
        List<StationInfo> stationInfo = stationInfoService.findChildStationByCityAndCounty(city,county);
        return Result.getListResult(StationInfo.class).setData(stationInfo).setMsg("根据(城市/区县)查询站点类型为子站点的站点信息成功").setCode(200);
    }

    /**
     * 查询所有区站点信息(新增废品价格时用)
     */
    @GetMapping("/findFatherStation")
    @ApiOperation(value = "查询所有区站点信息")
    public Result<List<StationInfo>> findFatherStation(){
        List<StationInfo> stationInfo = stationInfoService.findFatherStation();
        return Result.getListResult(StationInfo.class).setData(stationInfo).setMsg("查询所有区站点信息成功").setCode(200);
    }

    /**
     * 查询所有可分配骑手的站点
     */
    @GetMapping("/findAllStation")
    @ApiOperation(value = "查询所有可分配骑手的站点")
    public Result<List<StationInfo>> findAllStation(){
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_state", Data.STATION_STATE_OPEN);
        List<StationInfo> stationInfos = stationInfoService.list(queryWrapper);
        return Result.getListResult(StationInfo.class).setData(stationInfos).setMsg("查询所有可分配骑手的站点").setCode(200);
    }

    /**
     * 根据当前登录用户查询当前用户的站点
     */
    @GetMapping("/findStationByAdminId")
    @ApiOperation(value = "根据当前登录用户查询当前用户的站点")
    public Result<StationInfo> findStationByAdminId(@RequestHeader String token) throws Exception {
        //获取当前登录管理员的id
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println(adminId);
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("principal_id", adminId);
        StationInfo stationInfo = stationInfoService.getOne(queryWrapper);
        return Result.getResult(StationInfo.class).setData(stationInfo).setMsg("根据当前登录用户查询当前用户的站点成功").setCode(200);
    }

    /**
     * 根据当前登录用户查询当前用户的站点
     */
    @GetMapping("/findStationByAdminId2")
    @ApiOperation(value = "根据当前登录用户查询当前用户的站点")
    public Result<StationInfo> findStationByAdminId2(@RequestParam("adminId") Integer adminId){
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("principal_id", adminId);
        StationInfo stationInfo = stationInfoService.getOne(queryWrapper);
        return Result.getResult(StationInfo.class).setData(stationInfo).setMsg("根据当前登录用户查询当前用户的站点成功").setCode(200);
    }
}

